package com.tiny.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;

import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.tiny.model.Menu;
import com.tiny.model.MenuPermission;
import com.tiny.model.Permission;

public class PermissionController extends Controller {

	public void index() {
		render("/frame/permission.html");
	}
	
	public void query(){
		StringBuilder sql = new StringBuilder(" from permission where 1=1 ");

		if (StrKit.notBlank(this.getPara("permission"))) {
			sql.append(String.format(" and permission like '%s%%' ",
					this.getPara("permission")));
		}
		
		if (StrKit.notBlank(this.getPara("sort"))) {
			sql.append(String.format("%s %s %s", "order by",this.getPara("sort"),this.getPara("order")));
		}
		else{
			sql.append(" order by id desc ");
		}
		
		Page<Permission> page = Permission.dao.paginate(this.getParaToInt("page"), this.getParaToInt("rows"),"select * ", sql.toString());
		
		renderJson(page.getList());
	}
	
	public void save(){
		Permission permission = getModel(Permission.class);
		
		permission.set("permission", this.getPara("permission"));
		permission.set("description", this.getPara("text"));
		permission.set("create_time", new Date());
		if (StrKit.notBlank(this.getPara("parent_id"))) {
			permission.set("parent_id", this.getPara("parent_id"));
		}
		else{
			permission.set("parent_id", 0);
		}

		setAttr("success", permission.save());
		renderJson();
	}
	
	public void update(){
		Permission permission = getModel(Permission.class);
		
		permission.set("permission", this.getPara("permission"));
		permission.set("description", this.getPara("text"));
		permission.set("id", this.getPara("id"));
		if (StrKit.notBlank(this.getPara("parent_id"))) {
			permission.set("parent_id", this.getPara("parent_id"));
		}
		else{
			permission.set("parent_id", 0);
		}

		setAttr("success", permission.update());
		renderJson();
	}
	
	public void remove(){
		setAttr("success", Permission.dao.deleteById(this.getPara("id")));
		renderJson();
	}
	
	public void allPermission(){
		this.renderJson(selectShowPermissions(null));
	}
	
	public void rootPermission(){
		List<Permission> permissionList = Permission.dao.getAllPermission();
		this.renderJson(getRootPermissions(permissionList));
	}

	private List<Map<String, Object>> selectShowPermissions(Integer rootId) {

		List<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();
		// 获取所有权限
		List<Permission> permissionList = Permission.dao.getAllPermission();

		List<Permission> rootPermissions = getRootPermissions(permissionList);
		for (Permission permission : rootPermissions) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", permission.getId());
				map.put("permission", permission.getKey());
				map.put("text", permission.getDescription());
				List<Map<String, Object>> childrenPermissions = getChildrenPermissions(
						permissionList, permission.getId());
				map.put("children", childrenPermissions);
				maps.add(map);
		}
		return maps;
	}

	private List<Map<String, Object>> getChildrenPermissions(List<Permission> permissions,
			Integer parentId) {
		List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
		for (Permission permission : permissions) {
			if ((parentId == null && permission.getParentId() == null)
					|| (parentId != null && permission.getParentId() != null && permission
							.getParentId().intValue() == parentId.intValue())) {
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("id", permission.getId());
					map.put("permission", permission.getKey());
					map.put("text", permission.getDescription());
					mapList.add(map);
				}
			}
		return mapList;
	}

	private List<Permission> getRootPermissions(List<Permission> ls) {
		List<Permission> rootPermissions = new ArrayList<Permission>();
		for (Permission permission : ls) {
			if (permission.getParentId() == null||permission.getParentId() ==0) {
				rootPermissions.add(permission);
			}
		}
		return rootPermissions;
	}
}